<template>
  <div class="dashboard-container">
    <!-- 头部组件 -->
    <PageTools>
      <!-- 插入到 left 插槽位置 -->
      <template #left>
        <i class="el-icon-info" />
        <span>本月: 社保在缴 公积金在缴</span>
      </template>
      <!-- 插入到 right 插槽位置 -->
      <template #right>
        <el-button type="primary" size="small" @click="$router.push('/import')">导入excel</el-button>
        <el-button type="primary" size="small" disabled>导出excel</el-button>
        <!-- 新增员工按钮 -->
        <el-button type="primary" size="small" @click="visible=true">新增员工</el-button>
      </template>
    </PageTools>

    <!-- 表格区域 -->
    <el-card style="margin-top: 10px;">
      <el-table :data="list" border>
        <el-table-column label="序号" width="60" type="index" />
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="工号" prop="workNumber" />
        <!-- <el-table-column label="聘用形式" prop="formOfEmployment" /> -->
        <el-table-column label="聘用形式">
          <template #default="{ row }">
            <!-- {{ hireMap[row.formOfEmployment] }} -->
            {{ hireMap[row.formOfEmployment] }}
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName" />
        <!-- <el-table-column label="入职时间" width="200" prop="correctionTime" /> -->
        <el-table-column label="入职时间" width="200">
          <template #default="{row}">
            {{ formaDate(row.correctionTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="200">
          <template #default="{row}">
            <el-button type="text" size="small" @click="toDetail(row.id)">查看</el-button>
            <el-button type="text" size="small">分配角色</el-button>
            <el-button type="text" size="small" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <el-row type="flex" justify="center" align="middle" style="height: 60px">
      <el-pagination
        :current-page="params.page"
        :page-size="params.size"
        :total="total"
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
      />
    </el-row>

    <!-- 新增员工弹框组件 -->
    <Add :visible="visible" @changeVisibleTrue="changeVisibleTrue" @changeVisibleFalse="changeVisibleFalse" @fetchEmployeeList="fetchEmployeeList" />
  </div>
</template>

<script>
import PageTools from '@/components/PageTools'
import Add from './components/Add.vue'
import { getEmployeeList, delEmployee } from '@/api/employee'
import dayjs from 'dayjs'
export default {
  name: 'Dashboard',
  components: { PageTools, Add },
  data() {
    return {
      // 员工列表数据
      list: [],
      params: {
        page: 1, // 当前页码
        size: 10 // 每页的条数
      },
      total: 0, // 总条数
      // 枚举
      hireMap: {
        1: '正式',
        2: '非正式'
      },
      // 弹窗是否演示
      visible: false
    }
  },
  created() {
    this.fetchEmployeeList()
  },
  methods: {
    // 渲染列表
    async fetchEmployeeList() {
      const { rows, total } = await getEmployeeList(this.params)
      this.list = rows
      this.total = total
      // console.log(rows)
    },
    // 时间格式化
    formaDate(value) {
      return dayjs(value).format('YYYY-MM-DD')
    },
    // 展示第几页，发生变化的回调
    handleCurrentChange(newPage) {
      this.params.page = newPage
      this.fetchEmployeeList()
    },
    changeVisibleTrue() {
      this.visible = true
    },
    changeVisibleFalse() {
      this.visible = false
    },
    // 删除员工
    del(id) {
      // 1. 询问用户
      this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2. 调用接口
        await delEmployee(id)
        // 3. 提示用户
        this.$message.success('删除员工成功')
        // 删除最后一页最后一条数据时，页数减 1
        if (this.list.length === 1 && this.params.page > 1) {
          this.params.page--
        }
        // 4. 刷新列表
        this.fetchEmployeeList()
      }).catch(error => {
        console.log(error)
      })
    },
    // 跳转后查看详情
    toDetail(id) {
      this.$router.push({
        path: '/employeeDetail',
        query: {
          id
        }
      })
    }
  }
}
</script>
